package com.uniledger.exchange.wallet.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.rohit.recycleritemclicksupport.RecyclerItemClickSupport;
import com.uniledger.exchange.R;
import com.uniledger.exchange.wallet.Mnemonic;
import com.uniledger.exchange.wallet.VerifyBackupMnemonicWordsAdapter;
import com.uniledger.exchange.wallet.VerifyBackupSelectedMnemonicWordsAdapter;
import com.uniledger.exchange.wallet.WalletKey;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 验证助记词
 */
public class VerifyFragment extends MnemonicFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private WalletKey walletKey;
    private String mParam2;

    public VerifyFragment() {
        // Required empty public constructor
    }

    @BindView(R.id.rv_selected)
    RecyclerView rvSelected;
    @BindView(R.id.rv_mnemonic)
    RecyclerView rvMnemonic;

    VerifyBackupSelectedMnemonicWordsAdapter verifyBackupSelectedMnemonicWordsAdapter;
    VerifyBackupMnemonicWordsAdapter verifyBackupMnemonicWordsAdapter;

    List<String> backupList;

    public static VerifyFragment newInstance(WalletKey walletKey, String param2) {
        VerifyFragment fragment = new VerifyFragment();
        Bundle args = new Bundle();
        args.putParcelable(ARG_PARAM1, walletKey);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            walletKey = getArguments().getParcelable(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_verify, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @BindString(R.string.mnemonic_btn_verify)
    String btn_verify;

    @Override
    public void onResume() {
        super.onResume();
        listener.getButton().setText(btn_verify);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        backupList = Arrays.asList(walletKey.mnemonic.split("\\s+"));
        walletKey.mnemonicCount = backupList.size();
        List<Mnemonic> mnemonicList = new ArrayList<>();
        for (int i = 0; i < backupList.size(); i++) {
            mnemonicList.add(new Mnemonic(i, backupList.get(i)));
        }
        // 未选中单词
        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(getActivity());
        layoutManager.setFlexWrap(FlexWrap.WRAP);
        layoutManager.setAlignItems(AlignItems.STRETCH);
        rvMnemonic.setLayoutManager(layoutManager);
        verifyBackupMnemonicWordsAdapter = new VerifyBackupMnemonicWordsAdapter(getActivity());
        rvMnemonic.setAdapter(verifyBackupMnemonicWordsAdapter);
        verifyBackupMnemonicWordsAdapter.addItems(mnemonicList);

        // 已选中单词
        FlexboxLayoutManager layoutManager2 = new FlexboxLayoutManager(getActivity());
        layoutManager2.setFlexWrap(FlexWrap.WRAP);
        layoutManager2.setAlignItems(AlignItems.STRETCH);
        rvSelected.setLayoutManager(layoutManager2);
        verifyBackupSelectedMnemonicWordsAdapter = new VerifyBackupSelectedMnemonicWordsAdapter(getActivity());
        rvSelected.setAdapter(verifyBackupSelectedMnemonicWordsAdapter);
//        verifyBackupSelectedMnemonicWordsAdapter.addItems(mnemonicList);

        RecyclerItemClickSupport.addTo(rvMnemonic).setOnItemClickListener(new RecyclerItemClickSupport.OnItemClickListener() {
            @Override
            public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                Mnemonic mnemonicWord = verifyBackupMnemonicWordsAdapter.getList().get(position);
                if (verifyBackupMnemonicWordsAdapter.setSelection(mnemonicWord)) {
                    verifyBackupSelectedMnemonicWordsAdapter.addItem(mnemonicWord);
                }
            }
        });

        RecyclerItemClickSupport.addTo(rvSelected).setOnItemClickListener(new RecyclerItemClickSupport.OnItemClickListener() {
            @Override
            public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                List<Mnemonic> datas = verifyBackupMnemonicWordsAdapter.getList();
                List<Mnemonic> selectedList = verifyBackupSelectedMnemonicWordsAdapter.getList();
                int id = selectedList.get(position).id;
                Mnemonic verifyMnemonicWordTag;
                for (int i = 0; i < datas.size(); i++) {
                    verifyMnemonicWordTag = datas.get(i);
                    if (verifyMnemonicWordTag.id == id) {
                        verifyBackupMnemonicWordsAdapter.setUnselected(verifyMnemonicWordTag);
                        break;
                    }
                }
                verifyBackupSelectedMnemonicWordsAdapter.remove(position);
            }
        });

    }

    public static String join(List<Mnemonic> list) {
        StringBuilder sb = new StringBuilder();
        for (Mnemonic temp : list) {
            sb.append(temp.word);
        }
        return sb.toString();
    }

    @Override
    public void onClick(View v) {
        List<Mnemonic> selectedList = verifyBackupSelectedMnemonicWordsAdapter.getList();
        int count = 0;
        if (selectedList.size() == walletKey.mnemonicCount) {
            for (int i = 0; i < selectedList.size(); i++) {
                if (TextUtils.equals(selectedList.get(i).word, backupList.get(i))) {
                    count++;
                } else {
                    break;
                }
            }
            boolean isBackup = count == walletKey.mnemonicCount;
            if (isBackup) {//正确
                //TODO 备份助记词成功
                walletKey.isBackup = isBackup;
                listener.verify(walletKey);
            } else {//

            }
        } else {//助记词填写不完全

        }

    }


}
